﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric20180617
{
   public class Contraint
    {
        /// <summary>
        /// 泛型约束。 基类约束
        /// 1. 在泛型方法内可以直接使用基类的属性和方法
        /// 2. 调用的时候，只能传递基类或者基类的子类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tParameter"></param>
        public static void Show<T>(T tParameter) where T: People,IWork
        {
            Console.WriteLine("This is {0}, parameter={1}, type={2}", typeof(GenericMethod).Name, tParameter.GetType().Name, tParameter);

            Console.WriteLine("id={0}, name={1}", tParameter.Id,tParameter.Name);

            tParameter.work();
        }

        public static void ShowPeople(People p)
        {
            Console.WriteLine("This is {0}, parameter={1}, type={2}", typeof(GenericMethod).Name, p.GetType().Name, p);

            Console.WriteLine("id={0}, name={1}", p.Id, p.Name);
        }

        /// <summary>
        /// 接口约束
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tParameter"></param>
        public static void ShowInterface<T>(T tParameter) where T : ISport
        {

            tParameter.PinPang();
        }

        public static T Get<T>(T tParameter)
        {
            return tParameter;
        }

        public static T Get1<T>(T tParameter) where T: new()  //无参数构造,如果传入的是int，也能执行
        {
            T t = new T();
            return t;
        }

        public static T Get2<T>(T tParameter) where T : class  //引用类型
        {
            return null;
        }

        public static T Get3<T>(T tParameter) where T : struct  //引用类型和值类型
        {
            return default(T);
        }

        public static void Many<T>() where T:People,ISport,IWork,new()
        {

        }
    }
}
